package com.jhd.jiangxi.util;


import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jhd.jiangxi.R;
import com.jhd.jiangxi.ScanApplication;
import com.jhd.jiangxi.entity.LastInputEditText;

import java.util.List;

/**
 * Created by wuzd on 2018/5/17.
 */

public class PopWindow {
    private static ListView listView;
    public static void show(ScanApplication scanApplication,Context context, PopupWindow popup,boolean isCheck,TextView textView, List<String> name, List<String> id, int index){
        isCheck=false;
        listView = new ListView(context);
        listView.setDivider(null);//设置分割线
        listView.setVerticalScrollBarEnabled(false);//不显示滑动条
        listView.setAdapter(new MyAdapter(scanApplication,context,popup,isCheck,name,id,index,textView));
        if(index==1){
            textView.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {

                }
            });
        }
        if(popup.isShowing())popup.dismiss();
        popup.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
        popup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        popup.setWidth(textView.getWidth());
        popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popup.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.selector_unfocused));
        popup.setContentView(listView);//设置显示内容
        popup.setOutsideTouchable(true);//点击PopupWindow以外的区域自动关闭该窗口
        popup.showAsDropDown(textView,0,5);//显示在edit控件的下面0,0代表偏移量
        textView.setBackgroundResource(R.drawable.selector_focused);
        if(name.size()>0){
            popup.setOutsideTouchable(false);//点击PopupWindow以外的区域不关闭该窗口
        }
        popup.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                textView.setBackgroundResource(R.drawable.selector_unfocused);
            }
        });
    }
    //适配器
    private static class MyAdapter extends BaseAdapter {
        private List<String> name,id;private int index;private TextView textView;private Context context;
        private ScanApplication scanApplication;private boolean isCheck;private  PopupWindow popup;
        public MyAdapter(ScanApplication scanApplication,Context context, PopupWindow popup,boolean isCheck,List<String> name,List<String>id,int index,TextView textView) {
            this.name=name;
            this.id=id;
            this.textView=textView;
            this.index=index;
            this.context=context;
            this.isCheck=isCheck;
            this.popup=popup;
            this.scanApplication=scanApplication;
        }

        @Override
        public int getCount() {
            return name.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            HolderView holderView=null;
            if(convertView==null){
                convertView=View.inflate(context, R.layout.item_dialog_login,null);
                holderView=new HolderView();
                holderView.text=(TextView) convertView.findViewById(R.id.tvStorage);
                convertView.setTag(holderView);
            }else{
                holderView=(HolderView) convertView.getTag();
            }

            holderView.text.setText(name.get(position));
            if(index==0){//审批人(目前就审批人重名)
                if(scanApplication.getApprovalId()!=null){
                    if(id.get(position).equals(scanApplication.getApprovalId())){
                        holderView.text.setTextColor(textView.getResources().getColor(R.color.billChoosed));
                    }else{
                        holderView.text.setTextColor(textView.getResources().getColor(R.color.billTextChoose));
                    }
                }
            }else{
                if(textView.getText().toString().equals(name.get(position))){
                    holderView.text.setTextColor(textView.getResources().getColor(R.color.billChoosed));
                }else{
                    holderView.text.setTextColor(textView.getResources().getColor(R.color.billTextChoose));
                }
            }

            /*if(index==1){//供货单位
                if(scanApplication.getSaleDeptId()!=null){
                    if(id.get(position).equals(scanApplication.getSaleDeptId())){
                        holderView.text.setTextColor(textView.getResources().getColor(R.color.billChoosed));
                    }else{
                        holderView.text.setTextColor(textView.getResources().getColor(R.color.billTextChoose));
                    }
                }
            }else if(index==2){//疫苗属性
                if(textView.getText().toString().equals(name.get(position))){
                    holderView.text.setTextColor(textView.getResources().getColor(R.color.billChoosed));
                }else{
                    holderView.text.setTextColor(textView.getResources().getColor(R.color.billTextChoose));
                }
            }else if(index==3){//价格本
                if(scanApplication.getSaleDeptId()!=null){
                    if(id.get(position).equals(scanApplication.getSaleDeptId())){
                        holderView.text.setTextColor(textView.getResources().getColor(R.color.billChoosed));
                    }else{
                        holderView.text.setTextColor(textView.getResources().getColor(R.color.billTextChoose));
                    }
                }
            }*/

            holderView.text.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isCheck=true;
                    if(index==0){//审批人
                        scanApplication.setApprovalId(id.get(position));
                    }else if(index==1){//供货单位
                        scanApplication.setSaleDeptName(name.get(position));
                        scanApplication.setSaleDeptId(id.get(position));
                        EditTextDel.setCanDel((LastInputEditText) textView,false);
                    }else if(index==2){//疫苗类型
                        scanApplication.setCategory(id.get(position));
                    }else if(index==3){//价格本
                        scanApplication.setPriceNoIn(name.get(position));
                        scanApplication.setPriceBookMstId(id.get(position));
                    }else if(index==4){//储位
                        scanApplication.setStorageName(name.get(position));
                        scanApplication.setStorageId(id.get(position));
                    }else if(index==5){//移库类型和盘点类型.无需保存,最后可以直接获取,毕竟只有俩个
                        //scanApplication.setCheckType(name.get(position));
                    }else if(index==6){//移库类型和盘点类型报损人或者盘点人

                    }else if(index==7){//运输单位
                        scanApplication.setTransformName(name.get(position));
                        scanApplication.setTransformId(id.get(position));
                    }
                    textView.setText(name.get(position));
                    textView.setTextColor(textView.getResources().getColor(R.color.billTextChoose));
                    notifyDataSetChanged();
                    textView.setBackgroundResource(R.drawable.selector_unfocused);
                    popup.dismiss();
                }
            });
            return convertView;
        }
    }


        static class HolderView{//由于这里用户都是同一个人,就省略用户图像的刷新
            private TextView text;
        }
}
